package com.outdoor.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.outdoor.common.constant.MessageConstant;
import com.outdoor.common.context.BaseContext;
import com.outdoor.common.exception.DeletionNotAllowedException;
import com.outdoor.common.result.PageResult;
import com.outdoor.pojo.dto.EventPageQueryDTO;
import com.outdoor.pojo.dto.EventPublishDTO;
import com.outdoor.pojo.entity.Event;
import com.outdoor.pojo.entity.EventRegistration;
import com.outdoor.pojo.vo.EventVO;
import com.outdoor.server.mapper.EventMapper;
import com.outdoor.server.mapper.EventRegistrationMapper;
import com.outdoor.server.service.EventService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private EventRegistrationMapper eventRegistrationMapper;

    /**
     * 分页查询活动列表
     * @param eventPageQueryDTO
     * @return
     */
    public PageResult page(EventPageQueryDTO eventPageQueryDTO) {
        PageHelper.startPage(eventPageQueryDTO.getPage(), eventPageQueryDTO.getPageSize());
        Page<Event> events = eventMapper.pageQuery(eventPageQueryDTO);
        return new PageResult(events.getTotal(), events.getResult());
    }

    /**
     * 根据id查询活动详情
     * @param id
     * @return
     */
    public EventVO getById(Long id) {
        Event event = eventMapper.getById(id);
        //根据id查询报名人数和报名人列表
        List<EventRegistration> registrations = eventRegistrationMapper.getByEventId(id);
        //已支付的报名人列表
        List<EventRegistration> paidRegistrations = eventRegistrationMapper.getByEventIdAndStatus(id, EventRegistration.STATUS_PAY);
        //未支付的报名人列表
        List<EventRegistration> unpaidRegistrations = eventRegistrationMapper.getByEventIdAndStatus(id, EventRegistration.STATUS_NOT_PAY);
        int registrationCount = eventRegistrationMapper.getCountByEventId(id);
        EventVO eventVO = new EventVO();
        BeanUtils.copyProperties(event, eventVO);
        eventVO.setRegistrationCount(registrationCount);
        eventVO.setPaidRegistrations(paidRegistrations);
        eventVO.setUnpaidRegistrations(unpaidRegistrations);
        eventVO.setRegistrations(registrations);
        return eventVO;
    }

    /**
     * 新增活动
     * @param event
     */
    public void add(Event event) {
        event.setUid(BaseContext.getCurrentId());
        event.setStatus(Event.STATUS_DRAFT);
        eventMapper.insert(event);
    }


    /**
     * 发布活动
     * @param id
     */
    public void publish(Long id) {
        Event event = Event.builder()
                .id(id)
                .status(Event.STATUS_NORMAL)
                .build();
        eventMapper.update(event);
    }

    /**
     * 删除活动
     * @param id
     */
    public void delete(Long id) {
        Event event = eventMapper.getById(id);
        if (event.getStatus() != Event.STATUS_DRAFT) {
            throw new DeletionNotAllowedException(MessageConstant.DELETION_NOT_ALLOWED_BY_EVENT);
        }
        eventMapper.delete(id);
    }


    /**
     * 取消活动
     * @param id
     */
    public void cancel(Long id) {
        Event event = Event.builder()
                .id(id)
                .status(Event.STATUS_CANCEL)
                .build();
        eventMapper.update(event);
    }

    /**
     * 检查活动状态
     */
    public void checkEventStatus() {
        List<Event> events = eventMapper.findAll();
        for (Event event : events) {
            int finalStatus = event.getStatus(); // 保留当前状态
            //设置活动状态为报名结束
            if(event.getSignupEnd().isBefore(LocalDateTime.now())){
                finalStatus = Event.STATUS_DELETED;
            }
            //设置活动状态为活动满员
            int registrationCount = eventRegistrationMapper.getCountByEventId(event.getId());
            if(registrationCount >= event.getMaxMember()){
                finalStatus = Event.STATUS_FULL;
            }
            //设置活动状态为活动结束
            if(event.getEndTime().isBefore(LocalDateTime.now())){
                finalStatus = Event.STATUS_END;
            }
            event.setStatus(finalStatus);
            eventMapper.update(event);
        }
    }
}
